<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/23
 * Time: 11:58
 */
namespace app\admin\controller;
use app\admin\model\Column;
use app\admin\model\Jurs;
use app\admin\model\Subject;
use think\Controller;
use think\Db;
use think\Request;

class Jur extends Controller
{
    /**加载table页面
     * @return mixed
     */
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('jur/del')
            ->addTableHeaderAdd('jur/show_add')
            ->addTableHeaderSousuo('')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('jur_name','text','权限名称',null,'jur/feildedit')
            ->addTableColumn('table','text','权限表或栏目',null,'jur/feildedit')
            ->addTableColumn('descs','text','权限名(增/删/改/查)')
            ->addTableColumn('status','switch','状态',['启用','禁用'],'jur/change')
            ->addTableRightHeader('操作')
            ->addTableRight('edit','jur/show_edit','修改')
            ->addTableRight('del','jur/del','删除')
            ->addTablePage('jur/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        $data = Jurs::where('id','>',0);
        if($tiaojian){
            $data = $data->where('jur_name','like',"%$tiaojian%");
        }
        $data = $data->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            if($value['cid']){
                $shuju =Db::name(session('modular').'_'.'column')->where('id',$value['cid'])->find();
                if($shuju){
                    $value['table'] = $shuju['name'];
                }
            }elseif($value['model_id']){
                $shuju = Db::name(session('modular').'_'.'models')->where('id',$value['model_id'])->find();
                if($shuju){
                    $value['table'] = $shuju['model_name'];
                }
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**加载添加权限
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_add(Request $request){
        $column = Column::where('id','>',0)->select()->toArray();
        $cid = [];
        foreach ($column as $value){
            $cid[$value['id']] = $value['name'];
        }
        return NewBuilder::make('form')
            ->addAction('jur/add')
            ->addInput('jur_name','权限名称')
            ->addSelect('cid','栏目','',$cid,true)
            ->addSelect('descs','权限',null,['增','删','改','查'])
            ->addSwitch('status','状态',1)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**修改状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change(Request $request){
        $id = $request->param('id');
        $data = Jurs::where('id',$id)->findOrEmpty()->toArray();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Jurs::where('id',$id)->update([
            'status'=>$data['status']
        ])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**添加
     * @param Request $request
     */
    public function add(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $obj = Jurs::create($data);
        if(Subject::addJurToRole($obj->id)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**编辑权限
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $column = Column::where('id','>',0)->select()->toArray();
        $cid = [];
        $data = Jurs::where('id',$id)->findOrEmpty()->toArray();
        foreach ($column as $value){
            $cid[$value['id']] = $value['name'];
        }
        return NewBuilder::make('form')
            ->addAction('jur/edit')
            ->addNone('id',$id)
            ->addInput('jur_name','权限名称',$data['jur_name'])
            ->addSelect('cid','栏目',$data['cid'],$cid,true)
            ->addSelect('descs','权限',$data['descs'],['增','删','改','查'])
            ->addSwitch('status','状态',$data['status'])
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**编辑数据
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(Jurs::update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除权限
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $id = $request->param('id');
        if(!Subject::delJurToRoleAll($id)){
            return json_my_response('删除失败');
        }
        if(Jurs::where('id','in',$id)->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}